//============================================================================
// Name        : GSP295_Wk4_HashTable
// Author      : Ronald Ram
// Version     :
// Copyright   : GSP295 Wk4 Program - Hash Tables
// Description : Hash Table Implemented as Array in C++
//============================================================================



#include<iostream>
#include "HashTable.h"

using namespace std;
using namespace GSP295_Wk4;

//Method Declarations
void WelcomeMsg();
void Instructions();
void Command(char input);
void InsertToArray();
void DeleteFromArray();
void FindInArray();
void ClearArray();
void PrintArray();


bool quit = false; 
char addInsertDeleteFindClearPrintQuit;
string input;

HashTable *program;

int main()
{
	WelcomeMsg();

	program = new HashTable();

	do {
		Instructions();
		cout << "Enter Command: "; cin >> addInsertDeleteFindClearPrintQuit;
		system("cls");

		Command(addInsertDeleteFindClearPrintQuit);

	} while ( !quit );

	delete [] program;

	cout << "\n\n....Good Bye\n" << endl;
	cin.get(); //system("pause");
	return 0;
}


//Welcome Message
void WelcomeMsg()
{
	cout << "\n**************************************************" << endl;
	cout << "\t GSP295 Week 4: iLab Assignments" << endl;
	cout << "\n\t Title: Hash Tables - Program" << endl;
	cout << "\n\t Author: Ronald Ram" << endl;
	cout << "**************************************************\n\n\n" << endl;
}


//Instructions for Command keys to use
void Instructions()
{
	cout << "---------------------------------------" << endl;
	cout << "Command Options: " << endl;
	cout << "---------------------------------------" << endl;
	cout << "Insert[I] - Delete[D] - Find[F] - Print[P] - Clear[C]" << endl;
	cout << "Q -> Quit" << endl;
	cout << "---------------------------------------\n" << endl;
}


//Decide on which command to trigger
void Command(char input)
{
	if      ( input == 'I' || input == 'i' ) { InsertToArray();
	} 
	else if ( input == 'D' || input == 'd' ) { DeleteFromArray();
	}
	else if ( input == 'F' || input == 'f' ) { FindInArray();
	}
	else if ( input == 'C' || input == 'c' ) { ClearArray();
	}
	else if ( input == 'P' || input == 'p' ) { PrintArray();
	}
	else if ( input == 'Q' || input == 'q' ) { quit = true;
	}
}

//Add string to Array 
void InsertToArray()
{
	cout << "\nEnter a string: "; cin >> input;

	//Index = -2: Overflow
	//Index = -1: Collision
	//Index = 0+: index found, OK to INSERT
	Result result = program->Insert(input);

	if ( result.resultType == SUCCESS ) //success == 0+ 
	{
		cout << "\n\n======================================\n" << endl;
		cout << ">>> '" << input << "' - Successfully added @ Index: " << result.index << " <<<" << endl;
		cout << "\n\n======================================\n\n\n\n" << endl;

		//PrintArray();
	}
	else if ( result.resultType == COLLISION ) //success == -1 
	{
		cout << "\n\n======================================\n" << endl;
		cout << ">>> '" << input << "' - Successfully OVERRIDEN @ Index: " << result.index << " <<<" << endl;
		cout << "\n\n======================================\n\n\n\n" << endl;

		//PrintArray();
	}
	else if ( result.resultType == FAILED ) //success == -2 
	{
		cout << "\n\n======================================\n" << endl;
		cout << ">>> '" << input << "' - Insert FAILED - Array is Full, or will Overflow <<<" << endl;
		cout << "\n\n======================================\n\n\n\n" << endl;
	}
}


//Delete string from Array 
void DeleteFromArray()
{
	cout << "\nEnter a string: "; cin >> input;

	//Index = -2: No value found, OR Index out of bounds
	//Index = 0+: index found, OK to DELETE
	Result result = program->Delete(input);

	if ( result.resultType == SUCCESS ) //success == 0+  
	{
		cout << "\n\n======================================\n" << endl;
		cout << ">>> '" << input << "' - Successfully removed from Array @ Index: " << result.index << " <<<" << endl;
		cout << "\n\n======================================\n\n\n\n" << endl;

		//PrintArray();
	} 
	//success == -2 
	else 
	{
		cout << "\n\n======================================\n" << endl;
		cout << ">>> '" << input << "' - Delete FAILED - No Match Found <<<" << endl;
		cout << "\n\n======================================\n\n\n\n" << endl;
	}
}


//Find a string in the Array
void FindInArray()
{
	cout << "\nEnter a string: "; cin >> input;
	
	//Index = -2: No value found, OR Index out of bounds
	//Index = 0+: index found
	Result result = program->Find(input);

	if ( result.resultType == SUCCESS ) //if ( success ) 
	{
		cout << "\n\n======================================\n" << endl;
		cout << ">>> '" << input << "' - Found in Array at Index: " << result.index << " <<<" << endl;
		cout << "\n\n======================================\n\n\n\n" << endl;
	} 
	else 
	{
		cout << "\n\n======================================\n" << endl;
		cout << ">>> No Match Found <<<" << endl;
		cout << "\n\n======================================\n\n\n\n" << endl;
	}
}


//Clear all elements in Array
void ClearArray()
{
	program->Clear();
}


//Print all contents of array to screen
void PrintArray() 
{
	program->Print();
}


